package com.kuang.lesson01;

import java.sql.*;

import static java.lang.Class.forName;

//我的第一个JDBC程序
public class JdbcDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");//固定写法
//        用户信息和URL
        String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
//        链接成功，数据库对象 connection 代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
//        执行SQL的对象 statement 执行sql的对象
        Statement statement = connection.createStatement();
//        SQL对象执行SQL
        String sql = "SELECT * FROM users";
        ResultSet resultSet = statement.executeQuery(sql);//返回的结果集 结果集中封装了所有结果
        while(resultSet.next()){
            System.out.println("id= "+resultSet.getObject("id"));
            System.out.println("name= "+resultSet.getObject("NAME"));
            System.out.println("pwd= "+resultSet.getObject("PASSWORD"));
            System.out.println("email= "+resultSet.getObject("email"));
            System.out.println("birth= "+resultSet.getObject("birthday"));
        }
//        释放连接
        resultSet.close();
        statement.close();
        connection.close();

    }
}
